
import moment from 'moment';//时间操作
import { ref, nextTick } from "vue";
export const TableMixins = {

    methods: {
        //计算对象的生命周期
        to_age(t) {
            if (!t || t == "") {
                return ""
            }
            return moment.duration(moment(new Date()).diff(moment(t)), "ms").humanize()
        },
        query() {
            this.loading = true
            this.getItems().then((response) => {
                this.tableData = response.items;
                this.loading = false
            });

        },
        async openItemAsYaml(row, kind) {
            this.yamlVisible = true

            await nextTick();
            this.$refs.yamlEdit.loadYaml(kind, row.metadata.namespace, row.metadata.name)

        },
        //cpu转为以核为单位
        to_cpu_h(s) {
            if (s.endsWith("n")) {
                return (Number(s.replace("n", "")) / 1000000000.00).toFixed(2)
            }
            if (s.endsWith("m")) {
                return (Number(s.replace("m", "")) / 1000.00).toFixed(2)
            }
            if (s.endsWith("M")) {
                return (Number(s.replace("M", "")) / 1024.00).toFixed(2)
            }

            return s
        },
        //内存转为以G为单位
        to_memory_g(s) {
            if (s.endsWith("Ki")) {
                return (Number(s.replace("Ki", "")) / 1000000.00).toFixed(2)
            }
            if (s.endsWith("K")) {
                return (Number(s.replace("K", "")) / 1024.00/1024.00).toFixed(2)
            }
            if (s.endsWith("k")) {
                return (Number(s.replace("k", "")) / 1024.00/1024.00).toFixed(2)
            }
            if (s.endsWith("m")) {
                return (Number(s.replace("m", "")) / 1000.00 / 1024.00 /1024.00 / 1024.00).toFixed(2)
            }
            if (s.endsWith("M")) {
                return (Number(s.replace("M", "")) / 1024.00).toFixed(2)
            }if (s.endsWith("Mi")) {
                return (Number(s.replace("Mi", "")) / 1000.00).toFixed(2)
            }

            if (s.endsWith("Gi")) {
                return s.replace("Gi", "")
            }
            if (s.endsWith("G")) {
                return s.replace("G", "")
            }

            return Number(s) / 1024.00 / 1024.00 / 1024.00
        },
        bytesToG(s){
            return (Number(s) / 1024.00/1024.00/1024.00).toFixed(2)
        }
    }
}
